<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"

      >
    <ui:composition template="template.xhtml">
        <ui:define name="dialogs">

             <script type="text/javascript">
                function handleComplete(xhr, status, args) {
                    if(!args.showDialog) {
                       newUser.hide();
                    }
                }
            </script>


            <p:dialog header="Edição de usuário" widgetVar="editUser" modal="true" resizable="false" width="800">
                <h:form id="editUserForm">
                  <p:tabView>

                        <p:tab title="Dados do usuário">
                           <h:panelGrid columns="2">
                              <h:outputText value="Nome*: "  />
                              <p:inputText value="#{userBean.userSelected.name}"  style="width: 404px"/>
                              <h:outputText value="Login:"  />
                              <p:inputText value="#{userBean.userSelected.login}"  style="width: 170px" disabled="true" />
                           </h:panelGrid>
                           <h:panelGrid columns="4">
                              <h:outputText value="RG:"  />
                              <p:inputText value="#{userBean.userSelected.RG}" style="width: 170px" />
                              <h:outputText value="CPF:"  />
                              <p:inputText value="#{userBean.userSelected.CPF}" style="width: 170px" />
                              <h:outputText value="Email*:"  />
                              <p:inputText value="#{userBean.userSelected.email}" style="width: 170px" />
                              <h:outputText value="Telefone: "  />
                              <p:inputText value="#{userBean.userSelected.phone}" style="width: 170px" />
                           </h:panelGrid>
                        </p:tab>

                        <p:tab title="Obras">
                            <p:pickList value="#{userBean.dualListObras}" styleClass="largepicklist"
                                        var="obras" itemLabel="#{obras.name}" itemValue="#{obras}"
                                        converter="obraConverter">
                                <f:facet name="sourceCaption">Disponíveis</f:facet>
                                <f:facet name="targetCaption">Selecionados</f:facet>
                            </p:pickList>
                        </p:tab>

                        <p:tab title="Permissões" >
                           
                             <p:pickList value="#{userBean.dualListPermissions}" styleClass="largepicklist"
                                        var="permissions" itemLabel="#{permissions.name}" itemValue="#{permissions}"
                                        converter="permissionsConverter">
                                <f:facet name="sourceCaption">Disponíveis</f:facet>
                                <f:facet name="targetCaption">Selecionados</f:facet>
                            </p:pickList>
                       
                        </p:tab>
                  </p:tabView>
                    <p:commandButton value="Salvar" action="#{userBean.edit()}" update=":UserListform :growl" />
                </h:form>
            </p:dialog>

            <p:dialog header="Cadastro de usuário" widgetVar="newUser" modal="true" resizable="false">
               <h:form id="newUserform">
                <p:panel id="dadosUser" header="Dados do usuário" style="width: 510px">

                           <h:panelGrid columns="2">
                              <h:outputText value="Nome*: "  />
                              <p:inputText value="#{userBean.user.name}"  style="width: 404px"/>
                           </h:panelGrid>
                           <h:panelGrid columns="4">
                              <h:outputText value="RG:"  />
                              <p:inputText value="#{userBean.user.RG}" style="width: 170px" />
                              <h:outputText value="CPF:"  />
                              <p:inputText value="#{userBean.user.CPF}" style="width: 170px" />
                              <h:outputText value="Email*:"  />
                              <p:inputText value="#{userBean.user.email}" style="width: 170px" />
                              <h:outputText value="Telefone: "  />
                              <p:inputText value="#{userBean.user.phone}" style="width: 170px" />
                           </h:panelGrid>

                </p:panel>
                   <p:panel id="panelDadosAcessoUser" header="Dados de acesso">
                           <h:panelGrid columns="2">
                              <h:outputText value="Login*:"  />
                              <p:inputText value="#{userBean.user.login}"  style="width: 220px"/>
                              <h:outputText value="Senha*:"  />
                              <p:password value="#{userBean.user.password}" style="width: 220px"/>
                              <h:outputText value="Senha*:"  />
                              <p:password value="#{userBean.passwordCompare}" id="comparePass" style="width: 220px"/>
                              <p:watermark for="comparePass" value="Repita a senha digitada." />
                           </h:panelGrid>
                   </p:panel>
                   <p:commandButton value="Salvar" action="#{userBean.save()}" update=":UserListform :growl" oncomplete="handleComplete(xhr, status, args)"  />
               </h:form>
            </p:dialog>

        </ui:define>



        <ui:define name="content">
            <h:form id="UserListform">
              <h:panelGrid columns="2">
                        <h:outputText value="Nome:" styleClass="formLabel" style="width: 120px"/>
                        <p:inputText id="userName" style="width: 300px" value="#{centroCustoBean.centroCustoFilter}"
                                     onclick="jQuery(this).select()">
                            <p:ajax event="keyup" update="userList" />
                        </p:inputText>

                        <p:watermark for="userName" value="Digite o nome do usuário" />
                    </h:panelGrid>
              <h:panelGroup id="userList">
                  <p:dataTable var="u" value="#{userBean.listUsers}"
                               paginator="true" rows="10" rowKey="#{u.id}"
                                     emptyMessage="Nenhum centro de custo foi encontrado"
                                     selectionMode="single" selection="#{userBean.userSelected}">

                            <p:ajax event="rowSelect" update="@this"/>

                            <f:facet name="header">
                                Lista de usuários
                            </f:facet>

                            <p:column style="width: 70%">
                                <f:facet name="header">
                                    <h:outputText value="Nome" />
                                </f:facet>
                                <h:outputText value="#{u.name}" />
                            </p:column>

                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="E-mail" />
                                </f:facet>
                                <h:outputText value="#{u.email}" />
                            </p:column>

                             <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Telefone" />
                                </f:facet>
                                 <h:outputText value="#{u.phone}" />
                            </p:column>


                            <f:facet name="footer">
                                <p:commandButton icon="ui-icon ui-icon-plus" title="Adicionar usuário" update=":newUserform" oncomplete="newUser.show()"
                                                 disabled="#{not sharedPermissionManagedBean.hasPermission('CRIAR_USER')}"/>
                                
                                <p:commandButton icon="ui-icon ui-icon-pencil" title="Editar usuário" update=":editUserForm" onclick="editUser.show()"
                                                 disabled="#{(not sharedPermissionManagedBean.hasPermission('EDITAR_USER')) or (userBean.userSelected.id == null)}"
                                                  action="#{userBean.updateDualList()}"/>

                                <p:commandButton icon="ui-icon ui-icon-minus" title="Remover usuário"
                                                 disabled="#{(not sharedPermissionManagedBean.hasPermission('REMOVER_USER')) or (userBean.userSelected.id == null)}"
                                                  action="#{userBean.updateDualList()}"/>

                            </f:facet>
                        </p:dataTable>
                    </h:panelGroup>
            </h:form>
        </ui:define>
    </ui:composition>
</html>

